"""
 用类实现斐波那契数列的运算（编写自定义迭代器）
 0 1 1 2 3 5
"""
# class Fib_list:
#     def __init__(self):
#         self.before = 0
#         self.now = 1
#
#     def __iter__(self):
#         return self
#
#     def __next__(self):
#         result = self.now
#         self.before,self.now=self.now,self.before+self.now
#         return result
#
# a = Fib_list()
#
# print([next(a) for i in range(11)])

"""
 自定义异常类：
• 自定义异常能让异常更精准
• 自定义异常类：当list内元素长度超过10的时候抛出异常
• 自定义异常类：消息小于8时抛出异常
"""
# 方法一
# class List_Error(Exception):
#     pass
# lst1 = [1,2,3]
# if not 8<len(lst1)<10:
#     raise List_Error("列表长度小于8或超出10")
# else:
#     print("No Problem")

# 方法二：
# class List_Error(Exception):# 注：继承Exception的基类
#     def __init__(self,lst,message):
#         self.lst = lst
#         self.message = message
#
#     def __str__(self):
#         str1 = "list is " + str(self.lst)
#         str1 = str1 +"\n" + self.message
#         return str1
#
# try:
#     lst = [1,2,3,4,5,6]
#     if not 8 < len(lst) < 10:
#         raise List_Error(lst,"列表长度小于8或超出10！")
#
# except List_Error as e:
#     print("listError error:",e)
